package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryTargetTempReq;
import com.sg.dto.base.app.req.QueryTargetTempDetailReq;
import com.sg.dto.base.app.req.QueryTargetTempListReq;
import com.sg.entity.OmsTargetTemplate;
import com.sg.service.base.app.MOmsTargetTemplateService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsTargetTemplateMapper;
import com.wicket.okrframework.common.annotations.BanAuto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsTargetTemplateServiceImpl
        extends ServiceImpl<OmsTargetTemplateMapper, OmsTargetTemplate>
        implements MOmsTargetTemplateService {

    @Autowired
    OmsTargetTemplateMapper omsTargetTemplateMapper;

    /**
     * 3-1-01查指标模板列表[2210]
     * gen by moon at 10/15/2022, 10:00:45 PM
     */
    @Trace(operationName = "3-1-01查指标模板列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetTemplate> queryTargetTempList(QueryTargetTempListReq reqDto) {

        try {
            QueryWrapper<OmsTargetTemplate> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetTemplate.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetTemplate.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetTemplate.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsTargetTemplate>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-1-01查指标模板详情[2213]
     * gen by moon at 4/1/2023, 3:00:26 AM
     */
    @Trace(operationName = "3-1-01查指标模板详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTargetTemplate queryTargetTempDetail(QueryTargetTempDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            QueryWrapper<OmsTargetTemplate> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTargetTemplate> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsTargetTemplate retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-1-01新增指标模板[2216]
     * gen by moon at 10/27/2022, 4:06:40 PM
     */
    @Trace(operationName = "3-1-01新增指标模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    @BanAuto
    public String addTargetTemp(OmsTargetTemplate omsTargetTemplate) {
        omsTargetTemplate.setObjectiveTemplateId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTargetTemplate.setObjectiveTemplateNumber("T" + CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsTargetTemplate.setIsArchive("FALSE");
        omsTargetTemplate.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetTemplate.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsTargetTemplate != null && omsTargetTemplate.getSubjectLifeCycle() != null && omsTargetTemplate.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetTemplate.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsTargetTemplate.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetTemplate.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsTargetTemplate != null && omsTargetTemplate.getSubjectLifeCycle() != null && omsTargetTemplate.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetTemplate.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsTargetTemplate.setIsValid("FALSE");
        omsTargetTemplate.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsTargetTemplate.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTargetTemplate)) {
            return omsTargetTemplate.getObjectiveTemplateId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-1-01批量查指标模板[2711]
     * gen by moon at 12/17/2022, 9:04:47 PM
     */
    @Trace(operationName = "3-1-01批量查指标模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetTemplate> batchQueryTargetTemp(BatchQueryTargetTempReq reqDto) {

        try {
            QueryWrapper<OmsTargetTemplate> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getTargetTempList() == null || reqDto.getTargetTempList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("objective_template_id", reqDto.getTargetTempList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetTemplate.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetTemplate.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetTemplate.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsTargetTemplate>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-1-01修改指标模板[2761]
     * gen by moon at 10/15/2022, 10:00:48 PM
     */
    @Trace(operationName = "3-1-01修改指标模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTargetTemp(OmsTargetTemplate omsTargetTemplate) {

        return super.updateById(omsTargetTemplate);
    }

    /**
     * 3-1-01删除指标模板[2764]
     * gen by moon at 10/15/2022, 10:00:48 PM
     */
    @Trace(operationName = "3-1-01删除指标模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTargetTemp(String objectiveTemplateId) {

        return super.removeById(objectiveTemplateId);
    }

}

